//
//  SettingCell.m
//  彩票项目
//
//  Created by 伍银荣 on 2021/8/5.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "SettingCell.h"

@implementation SettingCell

+(instancetype)setttingCellWithTableView:(UITableView *)tableView andItem:(NSDictionary *)item {
    static NSString *cellID = @"";
    //解决cell重用的问题,因为cellType不同,所以可以直接将cellType作为cellid
    if (item[@"cellType"] && [item[@"cellType"] length] > 0) {
        cellID = item[@"cellType"];
    } else {
        cellID = @"setting_cell";
    }
    SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[SettingCell alloc]initWithStyle:[self loadCellWithItem:item] reuseIdentifier:cellID];
    }
    return cell;
}

+ (UITableViewCellStyle)loadCellWithItem:(NSDictionary *)item {
    if ([item[@"cellType"] isEqualToString:@"UITableViewCellStyleSubtitle"]) {
        return UITableViewCellStyleSubtitle;
    } else if ([item[@"cellType"] isEqualToString:@"UITableViewCellStyleValue1"]) {
        return UITableViewCellStyleValue1;
    } else if ([item[@"cellType"] isEqualToString:@"UITableViewCellStyleValue2"]) {
        return UITableViewCellStyleValue2;
    } else {
        return UITableViewCellStyleDefault;
    }
}

-(void)setItem:(NSDictionary *)item {
    _item = item;
    if (item[@"icon"]) {
        self.imageView.image = [UIImage imageNamed:item[@"icon"]];
    }
    //标题
    self.textLabel.text = item[@"title"];
    //子标题
    if ([item[@"isRed"] boolValue] == YES && item[@"isRed"]) {
        self.detailTextLabel.textColor = [UIColor redColor];
    }
    self.detailTextLabel.text = item[@"subTitle"];
    //根据字符串转换成类
    //获取字符串
    NSString *accessoryType = item[@"accessoryType"];
    //将字符串转化成类
    Class Clz = NSClassFromString(accessoryType);
    //初始化对象
    UIView *view = [[Clz alloc]init];
    if ([view isKindOfClass:[UIImageView class]]) {
        UIImageView *imgView = (UIImageView *)view;
        imgView.image = [UIImage imageNamed:item[@"accessoryContent"]];
        [imgView sizeToFit];
    } else if ([view isKindOfClass:[UISwitch class]]) {
        UISwitch *switcher = (UISwitch *)view;
        [switcher addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
        NSLog(@"home = %@",NSHomeDirectory());
        //获取开关状态
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//        BOOL switchIsOn = [[userDefaults valueForKey:self.item[@"SwitchKey"]] isEqualToString:@"Y"];
//        [switcher setOn:switchIsOn];
        switcher.on = [userDefaults boolForKey:self.item[@"SwitchKey"]];
    }
    //给cell赋值
    self.accessoryView = view;
}

- (void)switchValueChange:(UISwitch *)sender {
    //保存开关的状态
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:sender.isOn forKey:self.item[@"SwitchKey"]];
   // [userDefaults setObject:sender.isOn ? @"Y" : @"N" forKey:self.item[@"SwitchKey"]];
    [userDefaults synchronize]; //立即写入
    NSLog(@"home = %@",NSHomeDirectory());
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
